package com.company.LifeCycleThreadPool;

import com.company.ThreadPoolDemo.CustomizeThreadPool;

import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 作者  Administrator
 * 时间  2017/6/2 13:52
 * 文件  ThreadPoolsDemo
 * 描述  可暂停的带优先级测试的
 * 线程数量设置：通常核心线程数可以设为CPU数量+1，而最大线程数可以设为CPU的数量*2+1。
 */
public class PauseableThreadTest {
	public static void main(String[] args) {
		final PausableThreadPoolExecutor pausableThreadPoolExecutor =
				new PausableThreadPoolExecutor(1, 1, 0L,
						TimeUnit.SECONDS, new PriorityBlockingQueue<Runnable>());
		for (int i = 1; i <= 10; i++) {
			final int priority = i;
			pausableThreadPoolExecutor.execute(new CustomizeThreadPool.PriorityRunnable(priority) {
				@Override
				public void doSth() {
					/*runOnUiThread(new Runnable() {
						@Override
						public void run() {
							textView.setText(priority + "");
						}
					});*/
					System.out.println("优先级 "+priority);
					try {
						Thread.sleep(1000);
//						if (priority==5) pausableThreadPoolExecutor.shutdown();// 执行完第五个后 不让提交了
						if (priority==5) pausableThreadPoolExecutor.shutdownNow();// 执行完第五个后 已经提交的也不执行了
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			});
//			if (priority==5) pausableThreadPoolExecutor.shutdown();// 执行完第五个后 不让提交了
		}
	}
}
